/*
 * Copyright (c) 2017 <l_iupeiyu@qq.com> All rights reserved.
 */

package com.uyayo.portal.context.config;

import net.bull.javamelody.MonitoringFilter;
import net.bull.javamelody.MonitoringSpringAdvisor;
import net.bull.javamelody.SessionListener;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

import javax.servlet.Filter;
import javax.servlet.http.HttpSessionListener;

/**
 *
 */
//@Configuration
public class JavaMelodyConfig {

    @Bean
    public MonitoringSpringAdvisor monitoringSpringAdvisor(){
        MonitoringSpringAdvisor monitoringSpringAdvisor = new MonitoringSpringAdvisor();
        monitoringSpringAdvisor.setPointcut(jdkRegexpMethodPointcut());
        return monitoringSpringAdvisor;
    }

    @Bean
    public JdkRegexpMethodPointcut jdkRegexpMethodPointcut(){
        JdkRegexpMethodPointcut jdkRegexpMethodPointcut = new JdkRegexpMethodPointcut();
        jdkRegexpMethodPointcut.setPattern("com.uyayo.*.service.*");
        return  jdkRegexpMethodPointcut;
    }

    @Bean
    public HttpSessionListener javaMelodyListener(){
        return new SessionListener();
    }

    @Bean
    public Filter javaMelodyFilter(){
        return new MonitoringFilter();
    }

    @Bean
    public FilterRegistrationBean someFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(javaMelodyFilter());
        registration.addUrlPatterns("/monitoring/*");
        registration.addInitParameter("log", "true");
        registration.setName("monitoring");
        return registration;
    }
}
